import { INewsListState, INewsListAction, NewsListActionTypeName, INewsList } from '../../typings/interface'

const defaultState: INewsListState = {
  newsListState: localStorage.getItem('newsListState') ? JSON.parse(localStorage.getItem('newsListState')!) : [],
}

/**
{
   *  type: 'top'
   *  title: '头条'
   *  pageIndex: 0
   *  data: [
   *         [{id...}]  
   *  ]
   * }
 */

const newsListReducer = (state = defaultState, action: INewsListAction): INewsListState => {
  const { type, payload } = action

  switch (type) {
    case NewsListActionTypeName.ADD_NEWS_LIST:
      // @ts-igore
      return {
        newsListState: [...state.newsListState, payload as INewsList],
      }
    case NewsListActionTypeName.COLLECT_NEWS_LIST:
      return {
        ...state,
      }
    default:
      return state
  }
}

export default newsListReducer
